
package combat.example.server;

import gnu.cajo.invoke.Remote;
import gnu.cajo.utils.ItemServer;

import combat.example.server.protocol.implementation.Chat;
import combat.example.server.protocol.implementation.Scenario;
import combat.example.server.protocol.implementation.Simulation;


/**
 * @author Laurens Andriessen
 * @version 1.0
 */
public class ServerApplication {

	public static void main(String[] args) {
		try {
			// Configure CAJO remote server, listening on port 28000
			Remote.config(null, 28000, null, 0);

			try {
				// Set up static CAJO servers for classes with a single instance
				// (only one version/occurrence of the class)
				ItemServer.bind(Simulation.instance(), "Simulation");
				ItemServer.bind(Scenario.instance(), "Scenario");
				ItemServer.bind(Chat.instance(), "Chat");
			} catch (Exception e) {
				System.out.println("Error! Server already running!");
				e.printStackTrace();
				System.exit(0);
			}

			// Set up dynamic CAJO servers for classes with >1 instance

			// TODO Implement the Team-class so the errors go away (undo
			// abstract modifier @ Team.java and also add all missing members
			// and methods from the Team Interface in
			// combat.example.server.protocol.implementation.Team.java

			// Team Spectators
			/*
			 * Simulation.instance().addTeam( new
			 * Team(Simulation.instance().getTeams().size(), "Spectators"));
			 * ItemServer.bind(Simulation.instance().getTeams().get(0),
			 * "TeamSpectators");
			 */
			// Team Defense
			/*
			 * Simulation.instance().addTeam( new
			 * Team(Simulation.instance().getTeams().size(), "Defense"));
			 * ItemServer.bind(Simulation.instance().getTeams().get(1),
			 * "TeamDefense");
			 */
			// Team Offense
			/*
			 * Simulation.instance().addTeam( new
			 * Team(Simulation.instance().getTeams().size(), "Offense"));
			 * ItemServer.bind(Simulation.instance().getTeams().get(2),
			 * "TeamOffense");
			 */
		} catch (Exception e) {
			System.out.println("Error! Server creation failed...");
			e.printStackTrace();
			System.exit(0);
		}

		// Endless while-loop
		while (true) { /* Keep server running here (prevents auto-termination) */}
	}
}
